﻿//================================================================================
// Filename: PagePlugin.as
// Authour: Jacky Fong
// Description:
// Manage a page with different element and a common background
//
// Input Events
// - PagePlugin_SetBackground	
// - PagePlugin_ShowBackground
// - PagePlugin_AddPage
// - PagePlugin_AddPageElement
// - PagePlugin_AddPageShape
// - PagePlugin_AddPageDisplayObjectContainer
// - PagePlugin_ShowPage
//
// Output Events
// - PagePlugin_SetBackgroundComplete
// - PagePlugin_AddPageElementComplete
// - PagePlugin_ShowPageComplete
//================================================================================

package
{
    import flash.display.Loader;
    import flash.net.URLRequest;
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.display.Shape;	
	import flash.events.*;
	import IPlugin;
	import PluginManager;
	import PluginEvent;
	
	public class PagePlugin extends Sprite implements IPlugin
	{
		private static const _pluginName:String = "PagePlugin";
		private var _manager:PluginManager;
		private var _bgLoader:Loader;
		private var _pageList:Object;
		
		public function PagePlugin()
		{
			init();			
		}
		
		public function get pluginName():String
		{
			return _pluginName;
		}
				
		public function init():void
		{
			trace( "Init " + _pluginName );
			_pageList = new Object();
		}
		
		public function setManager( manager:PluginManager ):void
		{
			_manager = manager;
			_setupEventListeners();
		}

		// listen to plugin's self operations
		private function _setupEventListeners():void
		{
			if( _manager == null ) return;
			
			// plugin API
			_manager.addEventListener( "PagePlugin_SetBackground", _onSetBackground );
			_manager.addEventListener( "PagePlugin_ShowBackground", _onShowBackground );
			_manager.addEventListener( "PagePlugin_AddPage", _onAddPage );
			_manager.addEventListener( "PagePlugin_AddPageElement", _onAddPageElement );
			_manager.addEventListener( "PagePlugin_AddPageShape", _onAddPageShape );
			_manager.addEventListener( "PagePlugin_AddPageDisplayObjectContainer", _onAddPageDisplayObjectContainer );
			_manager.addEventListener( "PagePlugin_ShowPage", _onShowPage );
		}
		
        private function _loadBgComplete( evt:Event ):void
		{
			_manager.apiLog( this + " Load BG " + evt.target.content + " Complete: " + evt );
			_manager.dispatchEvent( new PluginEvent( "PagePlugin_SetBackgroundComplete" ) );	
        }
		
		private function _loadBgIoError( evt:IOErrorEvent ):void
		{
			_manager.apiLog( this + " Load BG IoError: " + evt );
		}
		
        private function _loadElementComplete( evt:Event ):void
		{
			_manager.apiLog( this + " Load Element " + evt.target.content + " Complete: " + evt );		
			_manager.dispatchEvent( new PluginEvent( "PagePlugin_AddPageElementComplete" ) );			
        }
		
		private function _loadElementIoError( evt:IOErrorEvent ):void
		{
			_manager.apiLog( this + " Load Element IoError: " + evt );
		}

		//==========================================================================
		// plugin API
		//==========================================================================
		private function _onSetBackground( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onSetBackground: " + evt );

			_bgLoader = new Loader();
			_bgLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, _loadBgComplete );			
			_bgLoader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, _loadBgIoError );
            var req:URLRequest = new URLRequest( evt.args.url );
            _bgLoader.load( req );
			_bgLoader.visible = false;
			_manager.apiAddChild( _bgLoader );
		}
		
		private function _onShowBackground( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onShowBackground: " + evt );
			_bgLoader.visible = evt.args.show;			
		}
		
		private function _onAddPage( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onAddPage: " + evt );
			
			_pageList[ evt.args.page ] = new Sprite();
			_pageList[ evt.args.page ].name = "page_" + evt.args.page;
			_pageList[ evt.args.page ].visible = false; // default hidden
			_manager.apiAddChild( _pageList[ evt.args.page ] );
		}

		private function _onAddPageElement( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onAddPageElement: " + evt );
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, _loadElementComplete );
			loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, _loadElementIoError );
            var req:URLRequest = new URLRequest( evt.args.url );
            loader.load( req );
			loader.x = evt.args.x;
			loader.y = evt.args.y;
			_pageList[ evt.args.page ].addChild( loader );			
		}
		
		private function _onAddPageShape( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onAddPageShape: " + evt );

            var s:Shape = new Shape();
            s.graphics.beginFill( evt.args.colour, evt.args.alpha );
            s.graphics.lineStyle( evt.args.borderSize, evt.args.borderColour, evt.args.borderAlpha );
			// todo: switch evt.args.shape to draw different shapes
            s.graphics.drawRect( evt.args.x, evt.args.y, evt.args.width, evt.args.height );
            s.graphics.endFill();
			_pageList[ evt.args.page ].addChild( s );
		}
		
		private function _onAddPageDisplayObjectContainer( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onAddPageDisplayObjectContainer: " + evt );
			
			_pageList[ evt.args.page ].addChild( evt.args.container );
		}
		
		private function _onShowPage( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onShowPage: " + evt );
			
			for( var p in _pageList ) {
				if( p == evt.args.page ) {
					_pageList[ p ].visible = true;
					_manager.dispatchEvent( new PluginEvent( "PagePlugin_ShowPageComplete", { page:evt.args.page } ) );	
				} else {
					_pageList[ p ].visible = false;
				}
			}
		}		
	}
}